import 'package:flutter/material.dart';
import 'package:ninetyfour/l10n/app_localizations.dart';

class DetailCancelView extends StatefulWidget {
  final Function(String) onReasonSelected;
  
  const DetailCancelView({super.key, required this.onReasonSelected});

  @override
  State<DetailCancelView> createState() => _DetailCancelViewState();
}

class _DetailCancelViewState extends State<DetailCancelView> {
  late List<String> _reasons;
  
  int _selectedIndex = -1;
  bool _isButtonEnabled = false;

  @override
  void initState() {
    super.initState();
    _reasons = [];
  }

  @override
  Widget build(BuildContext context) {
    _reasons = [
      AppLocalizations.of(context)!.cancelOrderReason1,
      AppLocalizations.of(context)!.cancelOrderReason2,
      AppLocalizations.of(context)!.cancelOrderReason3,
      AppLocalizations.of(context)!.cancelOrderReason4,
      AppLocalizations.of(context)!.cancelOrderReason5,
      AppLocalizations.of(context)!.otherReason
    ];
    
    return Container(
      height: 520,
      decoration: const BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(16),
          topRight: Radius.circular(16),
        ),
      ),
      child: Column(
        children: [
          Padding(
            padding: const EdgeInsets.only(top: 15, bottom: 15),
            child: Text(
              "${AppLocalizations.of(context)!.cancel}${AppLocalizations.of(context)!.order}" ,
              style: Theme.of(context).textTheme.titleMedium,
            ),
          ),
          Container(
            padding: const EdgeInsets.fromLTRB(20, 10, 0, 10),
            width: double.infinity,
            color: Colors.grey[200],
            alignment: Alignment.centerLeft,
            child: Text(AppLocalizations.of(context)!.cancelOrderReasonTitle),
          ),
          Expanded(
            child: ListView.builder(
              padding: EdgeInsets.zero, // 禁用默认内边距
              itemCount: _reasons.length,
              itemBuilder: (context, index) {
                return SizedBox(
                  height: 50, // 固定高度为50
                  child: InkWell(
                    onTap: () {
                      setState(() {
                        _selectedIndex = index;
                        _isButtonEnabled = true;
                      });
                    },
                    child: Padding(
                      padding: const EdgeInsets.only(top: 20, left: 16, right: 16), // 内容距离顶部20
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Text(
                            _reasons[index],
                            style: const TextStyle(fontSize: 13),
                          ),
                          _selectedIndex == index
                              ? const Icon(Icons.check_circle, color: Color(0xFF5D5FEE))
                              : const Icon(Icons.radio_button_unchecked),
                        ],
                      ),
                    ),
                  ),
                );
              },
            ),
          ),

          Padding(
            padding: const EdgeInsets.all(20),
            child: ElevatedButton(
              style: ElevatedButton.styleFrom(
                minimumSize: const Size(double.infinity, 44),
                backgroundColor: _isButtonEnabled ? Color(0xFF5D5FEE) : Colors.grey,
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(25),
                ),
              ),
              onPressed: _isButtonEnabled
                  ? () {
                      widget.onReasonSelected(_reasons[_selectedIndex]);
                      Navigator.of(context).pop();
                    }
                  : null,
              child: Text(AppLocalizations.of(context)!.submit, style: TextStyle(color: Colors.white,fontSize: 15)),
            ),
          ),
        ],
      ),
    );
  }
}